import { AppState, DeviceType } from './state'
import { AppMutationTypes } from './mutation-types'
import { setSidebarStatus } from '@/utils/cookies'

export type Mutations<S = AppState> = {
    [AppMutationTypes.TOGGLE_SIDEBAR](state: S, withoutAnimation: boolean): void
    [AppMutationTypes.CLOSE_SIDEBAR](state: S, withoutAnimation: boolean): void
    [AppMutationTypes.TOGGLE_DEVICE](state: S, device: DeviceType): void

}

export const mutations: Mutations = {
    [AppMutationTypes.TOGGLE_SIDEBAR](state: AppState, withoutAnimation: boolean) {
        state.sidebar.opened = !state.sidebar.opened
        state.sidebar.withoutAnimation = withoutAnimation
        if (state.sidebar.opened) {
            setSidebarStatus('opened')
        } else {
            setSidebarStatus('closed')
        }
    },

    [AppMutationTypes.CLOSE_SIDEBAR](state: AppState, withoutAnimation: boolean) {
        state.sidebar.opened = false
        state.sidebar.withoutAnimation = withoutAnimation
        setSidebarStatus('closed')
    },

    [AppMutationTypes.TOGGLE_DEVICE](state: AppState, device: DeviceType) {
        state.device = device
    }
}
